package com.mybase.app.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mybase.app.R;
import com.mybase.app.Utils.UserUtil;
import com.mybase.app.entity.User;

/**
 * Created by happy_000 on 2014/10/20.
 */
public class LoginActivity  extends Activity implements View.OnClickListener{
    private EditText name;
    private EditText passwd;
    private CheckBox savepw;
    private Button submit ,regiter;
    private User user;
    private boolean isSave=false;
    private String URL="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        user = UserUtil.getUser(this);
        name = (EditText) findViewById(R.id.login_name);
        passwd = (EditText) findViewById(R.id.login_passed);
        savepw = (CheckBox) findViewById(R.id.login_savepw);
        submit = (Button) findViewById(R.id.login_submit);
        regiter = (Button) findViewById(R.id.login_regiter);
        savepw.setOnClickListener(this);
        regiter.setOnClickListener(this);
        passwd.addTextChangedListener(new MyTextWatch());
      //  submit.setOnClickListener(this);
        name.setText(user.getName());
        passwd.setText(user.getPasswd());
    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.login_regiter :
                Intent intent=new Intent(LoginActivity.this,RegiterActivity.class);
                startActivity(intent);
                break;
            case R.id.login_savepw :
                isSave=!isSave;
                if (isSave){
                    user.setSavePasswd(true);
                }else {
                    user.setSavePasswd(false);
                }
                break;
            case R.id.login_submit :
                loginCheck();

            break;
        }
    }

    private void loginCheck() {
        String strName=name.getText().toString();
        String strPasswd=passwd.getText().toString();
        if (strName.equals("")||strPasswd.equals(""))
        {
            return ;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                RequestParams params = new RequestParams();
//                params.addBodyParameter("username", name);
//                params.addBodyParameter("pass", passwd);
//                params.addBodyParameter("mobile", phone);
//                params.addBodyParameter("sex", sex);
                params.addBodyParameter("birthday","2015-03-05");
                HttpUtils http = new HttpUtils();
                http.send(HttpRequest.HttpMethod.POST, URL, params, new RequestCallBack<String >() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {

                        Intent intent1=new Intent(LoginActivity.this,HomeActivity.class);
                        startActivity(intent1);
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        //todo
                        Intent intent1=new Intent(LoginActivity.this,HomeActivity.class);
                        startActivity(intent1);
                    }
                });
            }
        }).start();
    }

    class MyTextWatch implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!s.equals(""))
            {
                submit.setBackgroundResource(R.drawable.btn_check);
                submit.setOnClickListener(LoginActivity.this);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
    public void showMessage(String string){
        Toast.makeText(this, string,Toast.LENGTH_SHORT).show();
    }


}
